package LeetCode.leetcode543;

import java.util.LinkedList;
import java.util.Queue;

class Solution {
    int ans = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        reversedDiameterOfBinaryTree(root);
        return ans - 1;
        
    }
    public int reversedDiameterOfBinaryTree(TreeNode root) {
        if (root == null) return 0;
        int left = diameterOfBinaryTree(root.left);
        int right = diameterOfBinaryTree(root.right);
        ans = Math.max(ans, left + right + 1);
        return Math.max(left, right) + 1;
    }
}